﻿using Ocean.Outpatient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊就诊记录
    /// </summary>
    public class OtpMdtrtRecService : OutpatientAppService, IOtpMdtrtRecService
    {
        private IRepository<OtpMdtrtRec, Guid> _otpMdtrtRecRepository;
        private IOtpQueueRecService _otpNavRecService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="otpMdtrtRecRepository"></param>
        public OtpMdtrtRecService(IRepository<OtpMdtrtRec, Guid> otpMdtrtRecRepository
            , IOtpQueueRecService otpNavRecService)
        {
            _otpMdtrtRecRepository = otpMdtrtRecRepository;
            _otpNavRecService = otpNavRecService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<OtpMdtrtRecDto>> GetListAsync(OtpMdtrtRecInput input)
        {
            var iQuery = await _otpMdtrtRecRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.MdtrtSn), i => i.MdtrtSn == input.MdtrtSn);
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.DeptCodg), i => i.DeptCodg == input.DeptCodg);
            iQuery = iQuery.WhereIf(input.DrId != null, i => i.DrId == input.DrId);
            iQuery = iQuery.WhereIf(input.MdtrtTime.HasValue, i => i.MdtrtTime.Value.Date == input.MdtrtTime);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OtpMdtrtRec>, List<OtpMdtrtRecDto>>(iQuery.ToList());

                return new PagedResultDto<OtpMdtrtRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OtpMdtrtRec>, List<OtpMdtrtRecDto>>(items.ToList());

                return new PagedResultDto<OtpMdtrtRecDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 提交门诊就诊
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<OtpMdtrtRecDto> PostAsync(OtpMdtrtRecCreateDto input)
        {
            OtpQueueRecInput otpNavRecInput = new OtpQueueRecInput();
            otpNavRecInput.MdtrtSn = input.MdtrtSn;
            var otpNavRecs = await _otpNavRecService.GetListAsync(otpNavRecInput);
            var otpNavRec = otpNavRecs.Items.FirstOrDefault();

            if (otpNavRec != null)
            {
                OtpMdtrtRec otpMdtrtRec = new OtpMdtrtRec(otpNavRecInput.MdtrtSn
                    , otpNavRec.PatnId, otpNavRec.OtpNo, otpNavRec.PatnName
                    , otpNavRec.DeptId, otpNavRec.DrId);
                otpMdtrtRec = await _otpMdtrtRecRepository.InsertAsync(otpMdtrtRec);
                var itemDto = ObjectMapper.Map<OtpMdtrtRec, OtpMdtrtRecDto>(otpMdtrtRec);

                return itemDto;
            }
            else
            { 
                throw new UserFriendlyException("otpNavRec == null");
            }
        }
    }
}
